﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using HalalCheck.CustomViews;
using HalalCheck.Resources;
using HalalCheck.Services;
using SpotOffer.Resource;
using Xamarin.Forms;
using XLabs.Forms.Controls;
using XLabs.Forms.Mvvm;

namespace HalalCheck.View
{
    public class QuranPage : BasePage
    {
        public HybridWebView WebviewQuran { get; set; }
        private const int ViewPadding = 1;
        public QuranPage()
        {
            Title = HalalStrings.Quran;

            var layout = new RelativeLayout
            {
                VerticalOptions = LayoutOptions.FillAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand,
     
            };
            //  layout.Children.Add(new TileViewHalal(), Constraint.Constant(0), Constraint.Constant(0), Constraint.RelativeToParent((p => p.Bounds.Width)), Constraint.RelativeToParent((p) => p.Bounds.Height));

            var halalchecklogo = new Image
            {
                Source = "centerlogo.png"
            };
            layout.Children.Add(halalchecklogo, Constraint.RelativeToParent((p) => (p.Bounds.Width - 200) / 2), Constraint.RelativeToParent((p) => (p.Bounds.Height - 200) / 2), Constraint.RelativeToParent((p => 200)), Constraint.RelativeToParent((p) => 200));

            var quranLayout = new RelativeLayout
            {
                BackgroundColor = Color.Transparent
            };
            WebviewQuran = new HybridWebView
            {
                VerticalOptions = LayoutOptions.FillAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                BackgroundColor = Color.Transparent
               
            };
            WebviewQuran.LoadFinished += HandleLoadFinished;



            quranLayout.Children.Add(WebviewQuran, Constraint.Constant(0), Constraint.Constant(0), Constraint.RelativeToParent((p) => p.Bounds.Width), Constraint.RelativeToParent((p) => p.Bounds.Height - (50 + ViewPadding * 2) - AppMargins.ItemMargin - AppMargins.ItemPadding));


            var btnPlay = new HalalButton
             {
                 Text = "\u25B7",
             };
            quranLayout.Children.Add(btnPlay, Constraint.Constant(0), Constraint.RelativeToView(WebviewQuran, (p, s) => s.Bounds.Bottom), Constraint.Constant(50));

            btnPlay.Clicked += ((o, a) =>
            {
                WebviewQuran.CallJsFunction("gq.player.play()");
            });

            var btnStop = new HalalButton
            {
                Text = "\u25A0",
            };
            quranLayout.Children.Add(btnStop, Constraint.RelativeToView(btnPlay, (p, s) => s.Bounds.Right + ViewPadding), Constraint.RelativeToView(WebviewQuran, (p, s) => s.Bounds.Bottom), Constraint.Constant(50));
            btnStop.Clicked += ((o, a) =>
            {          
                WebviewQuran.CallJsFunction(Device.OS == TargetPlatform.iOS ? "gq.player.pause()" : "gq.player.stop()");
            });

            var btnPrev = new HalalButton
            {
                Text = "<<",
            };
            quranLayout.Children.Add(btnPrev, Constraint.RelativeToView(btnStop, (p, s) => s.Bounds.Right + ViewPadding), Constraint.RelativeToView(WebviewQuran, (p, s) => s.Bounds.Bottom), Constraint.Constant(50));

            btnPrev.Clicked += ((o, a) =>
            {
					WebviewQuran.CallJsFunction("gq.player.prev()");
            });

            var btnNext = new HalalButton
            {
                Text = ">>",
            };
            quranLayout.Children.Add(btnNext, Constraint.RelativeToView(btnPrev, (p, s) => s.Bounds.Right + ViewPadding), Constraint.RelativeToView(WebviewQuran, (p, s) => s.Bounds.Bottom), Constraint.Constant(50));
            btnNext.Clicked += ((o, a) =>
            {
					WebviewQuran.CallJsFunction("gq.player.next()");

            });

            var btnPrevP = new HalalButton
            {
                Text = "|<<",
            };
            quranLayout.Children.Add(btnPrevP, Constraint.RelativeToView(btnNext, (p, s) => s.Bounds.Right + ViewPadding), Constraint.RelativeToView(WebviewQuran, (p, s) => s.Bounds.Bottom), Constraint.Constant(50));
            btnPrevP.Clicked += ((o, a) =>
            {
                WebviewQuran.CallJsFunction("gq.prevPage()");
                //  WebviewQuran.CallJsFunction("gq.player.play()"); 
            });

            var btnNextP = new HalalButton
            {
                Text = ">>|",

            };
            quranLayout.Children.Add(btnNextP, Constraint.RelativeToView(btnPrevP, (p, s) => s.Bounds.Right + ViewPadding), Constraint.RelativeToView(WebviewQuran, (p, s) => s.Bounds.Bottom), Constraint.Constant(50));
            btnNextP.Clicked += ((o, a) =>
            {
					WebviewQuran.CallJsFunction("gq.nextPage()");
            });
            var whiteBackground = AppViews.WhiteRounded();
            layout.Children.Add(whiteBackground, Constraint.Constant(AppMargins.ItemPadding), Constraint.Constant(AppMargins.ItemPadding), Constraint.RelativeToParent((p) => p.Bounds.Width - AppMargins.ItemPadding * 2), Constraint.RelativeToParent((p) => p.Bounds.Height - AppMargins.ItemPadding * 2));
            layout.Children.Add(quranLayout, Constraint.Constant(AppMargins.ItemMargin),
                Constraint.Constant(AppMargins.ItemMargin), Constraint.RelativeToParent((p) => p.Bounds.Width - AppMargins.ItemMargin * 2),
                Constraint.RelativeToParent((p) => p.Bounds.Height));
            WebviewQuran.RegisterCallback("loadstart", Loadstarted);
            WebviewQuran.RegisterCallback("loadeddata", Loadfinished);

            Content = layout;
        }

        private void Loadfinished(string obj)
        {
            Dialogs.HideLoading();
        }

        private void Loadstarted(string obj)
        {
            Dialogs.ShowLoading();
        }


        private void HandleLoadFinished(object sender, EventArgs e)
        {
            Dialogs.HideLoading();
        }


        protected override void OnAppearing()
        {
            base.OnAppearing();
            Dialogs.ShowLoading();
            WebviewQuran.LoadFromContent("QURAN/index.html");
        }

        protected override void OnDisappearing()
        {
            base.OnDisappearing();
            WebviewQuran.CallJsFunction("gq.player.stop()");
            WebviewQuran.CallJsFunction("gq.player.destroy()");
          
        }
    }
}
